package com.xiaoma.h5testdemo.business.main.ui

import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.xiaoma.common.adapter.listener.OnXmItemClickListener
import com.xiaoma.h5testdemo.business.main.adapter.WebUrlAdapter
import com.xiaoma.h5testdemo.business.main.vm.HostActVm
import com.xiaoma.h5testdemo.business.main.vm.MainFrgVm
import com.xiaoma.h5testdemo.common.base.BaseSupportFrgBackFragment
import com.xiaoma.h5testdemo.databinding.FragmentMainBinding

/**
 * author: liangjingbo
 * date: 2024/4/18
 * describe:
 */
class MainFragment :
    BaseSupportFrgBackFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
    private val mHostVm by activityViewModels<HostActVm>()
    private val mMainVm by viewModels<MainFrgVm>()
    private val mAdapter = WebUrlAdapter()
    override fun initView(savedInstanceState: Bundle?) {
        binding.rvList.apply {
            layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            addItemDecoration(object : ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State) {
                    val pos = parent.getChildLayoutPosition(view)
                    val t = if (pos == 0) 0 else 20
                    outRect.set(0, t, 0, 0)
                }
            })
            adapter = mAdapter
        }
    }

    override fun initEvent(savedInstanceState: Bundle?) {
        binding.urlEdit.setOnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
                val inputMethodManager = requireContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.hideSoftInputFromWindow(v.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS)
            }
        }
        binding.urlLoad.setOnClickListener {
            var url: String = binding.urlEdit.editableText.toString()
            if (!url.contains("://") && !url.startsWith("javascript:")) {
                url = "https://$url"
            }
            binding.urlEdit.clearFocus()
            binding.urlEdit.setText("")
            mHostVm.handleJumpWeb(url)
        }

        binding.urlEdit.setOnEditorActionListener { v, actionId, event ->
            if (actionId === EditorInfo.IME_ACTION_SEARCH) {
                var url: String = binding.urlEdit.editableText.toString()
                if (!url.contains("://") && !url.startsWith("javascript:")) {
                    url = "https://$url"
                }
                binding.urlEdit.clearFocus()
                binding.urlEdit.setText("")
                mHostVm.handleJumpWeb(url)
            }
            true
        }
        mMainVm.collectListLiveData.observe(this) { vm ->
            vm.onData {
                mAdapter.setNewInstance(it)
            }
        }

        mAdapter.setOnItemClickListener { position ->
            val data = mAdapter.getData()[position]
            if (!data.url.isNullOrEmpty()) {
                mHostVm.handleJumpWeb(data.url)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mMainVm.updateCollectList()
    }
}